/**
 * 网络请求配置
 */
 import axios from "axios";
 import { message} from 'antd';
//  axios.defaults.timeout = 100000;
//  axios.defaults.baseURL = "http://192.168.3.15:8301/";
 

 const service = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: "http://192.168.3.15:8301/",
     //baseURL: 'http://192.168.3.133:8875',
    // 超时
    timeout: 10000
  })


 /**
  * http request 拦截器
  */
  service.interceptors.request.use(
   (config) => {
    //  config.data = JSON.stringify(config.data);
     config.headers = {
       "Content-Type": "application/json",
       "Access-Control-Allow-Origin": "*",
       "Access-Control-Allow-Headers": "Authorization,Origin, X-Requested-With, Content-Type, Accept",
       Authorization: "Basic eGlhb3NoaToxMjM0NTY=" ,
     };

     return config;
   },
   (error) => {
     return Promise.reject(error);
   }
 );
 
 /**
  * http response 拦截器
  */
  service.interceptors.response.use(
   (response:any) => {

    console.log(3,response);

    //console.log(1234,response);
    //  if(response.data.code !=='200'){
    //     msag(response.data)
    //  }
    //console.log(response);
    //  if (response.data.errCode === 2) {
    //    console.log("过期");
    //  }
     return response;
   },
   (error:any) => {
       console.log("请求出错：", error.response);
         msag(error.response.data)
       
   
   }
 );


 export default service


export function login(params : any ){
    
    let url:string ="/reslogin/oauth/token"

    return new Promise((resolve, reject) => {
        service.post(url, null, {
            params: params,
          }).then((response) => {
            resolve(response);
          })
          .catch((error) => {
            reject(error);
          });
      }); 

 }


 
 /**
  * 封装get方法
  * @param url  请求url
  * @param params  请求参数
  * @returns {Promise}
  */
 export function get(url :string , params = {}) {
   return new Promise((resolve, reject) => {
    service.get(url , {
         params: params,
       }).then((response) => {
         resolve(response.data);
       })
       .catch((error) => {
         reject(error);
       });
   });
 }
 
 /**
  * 封装post请求
  * @param url
  * @param data
  * @returns {Promise}
  */
 
 export function post(url:string, data:object) {
   return new Promise((resolve, reject) => {
    service.post(url, data).then(
       (response) => {
         //关闭进度条
         resolve(response.data);
       },
       (err) => {
         reject(err);
       }
     );
   });
 }
 
 /**
  * 封装patch请求
  * @param url
  * @param data
  * @returns {Promise}
  */
 export function patch(url :string, data = {}) {
   return new Promise((resolve, reject) => {
    service.patch(url, data).then(
       (response) => {
         resolve(response.data);
       },
       (err) => {
         msag(err);
         reject(err);
       }
     );
   });
 }
 
 /**
  * 封装put请求
  * @param url
  * @param data
  * @returns {Promise}
  */
 
 export function put(url:string, data = {}) {
   return new Promise((resolve, reject) => {
    service.put(url, data).then(
       (response) => {
         resolve(response.data);
       },
       (err) => {
         msag(err);
         reject(err);
       }
     );
   });
 }
 


 function error(msg:string){
  message.error({
    content: msg,
    className: 'custom-class',
    style: {
      marginTop: '20vh',
    },
  },1);
};
 
  /**
   * 失败得提示any
   * @param {} data 
   */
 function msag(data:any) {
   if (data && data.code != null) {
     switch (data.code) {
       case "400":
         alert(data.msg);
         break;
       case "401":
         alert("未授权，请登录");
         break;
 
       case "403":
         alert("拒绝访问");
         break;
 
       case "404":
         alert("请求地址出错");
         break;
 
       case "408":
         alert("请求超时");
         break;
 
       case "500":
        error(data.msg)
         break;
 
       case "501":
         alert("服务未实现");
         break;
 
       case "502":
         alert("网关错误");
         break;
 
       case "503":
         alert("服务不可用");
         break;
 
       case "504":
         alert("网关超时");
         break;
 
       case "505":
         alert("HTTP版本不受支持");
         break;
       default:
     }
   }


 }
 

 
